﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Elentok.Common.Async
{
  /// <summary>
  /// A simple asynchronous task that gets the method to run in the background
  /// in the constructor.
  /// </summary>
  public class DelegateAsyncTask : BaseAsyncTask
  {
    #region Fields

    private Action<IAsyncProgressNotifier> runInBackground;

    #endregion

    #region Ctor

    /// <summary>
    /// Initializes a new instance of the <see cref="DelegateAsyncTask"/> class.
    /// </summary>
    /// <param name="initialMessage">The initial message to display.</param>
    /// <param name="runInBackground">The run in background.</param>
    public DelegateAsyncTask(string initialMessage, Action<IAsyncProgressNotifier> runInBackground)
      : base(initialMessage)
    {
      this.runInBackground = runInBackground;
    }

    #endregion

    #region BaseAsyncTask Members

    /// <summary>
    /// When implemented by an inheriting class, this method will run in the background.
    /// </summary>
    protected override void RunInBackground()
    {
      this.runInBackground(this);
    }

    #endregion
  }
}
